<?php
/**
 * Created by JetBrains PhpStorm.
 * User: evan
 * Date: 13-3-5
 * Time: 下午7:12
 * To change this template use File | Settings | File Templates.
 */
class System extends MY_Agent_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    public function profile()
    {
        $this->load->model('model_staff', 'staff');
        $staffInfo = $this->staff->getStaffById($this->uInfo['staff_id']);


        $this->load->model('Model_depart', 'depart');
        $departmentInfo = $this->depart->lists(0, 1000, array('is_del' => DEPART_IS_DELETE_NORMAL));

        $data = array(
            'data' => $staffInfo,
            'department' => sortData($departmentInfo, 0, 'depart_id'),
        );
        $this->load->view('agent/system/profile', $data);
    }

    public function saveProfile()
    {
        $password = $this->input->get_post('password');
        $realname = $this->input->get_post('realname');
        $sex = $this->input->get_post('sex');
        $mobile = $this->input->get_post('mobile');
        $idCard = $this->input->get_post('id_card');
        $email = $this->input->get_post('email');
        $departId = $this->input->get_post('depart_id');
        $descr = $this->input->get_post('descr');
        //$competence = $this->input->get_post('competence');
        $staffId = $this->input->get_post('staff_id');

        $data = array(
            'realname' => $realname,
            'mobile' => $mobile,
            'email' => $email,
            'sex' => $sex,
            'depart_id' => $departId,
            'descr' => $descr,
            'id_card' => $idCard,
            'is_del' => '0',
        );
        $password && $data['password'] = md5($password);

        $this->load->model('model_staff', 'staff');
        $lastId = $this->staff->save($data, $staffId ? $staffId : null);

        if (!empty ($staffId)) {
            $lastId = $staffId;
        }

        $this->load->helper('url');
        redirect(url('admin').'system/profile', 'refresh');
    }

    public function reset_password()
    {
        $this->load->view('agent/system/reset_password');
    }

    public function changePassword()
    {
        $currentPassword = $this->input->get_post('current_password');
        $newPassword = $this->input->get_post('new_password');
        $repatPassword = $this->input->get_post('repat_password');

        if ($newPassword != $repatPassword) {
            show_error('两次输入的密码错误！');
        }

        $this->load->model('model_staff', 'staff');
        $status = $this->staff->getStaffById($this->uInfo['staff_id'], '*', array('password' => md5($currentPassword)));

        if (!$status) {
            show_error('当前用户密码错误!');
        }

        $this->staff->save(array('password' => md5(trim($newPassword))), $this->uInfo['staff_id']);

        echo '<script>alert("修改密码成功!");window.location.href="/system/reset_password"</script>';
    }
}